package com.ktjiaoyu.controller;

import com.ktjiaoyu.pojo.SysUser;
import com.ktjiaoyu.service.SysUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Resource
    SysUserService sysUserService;

    @PostMapping("/login")
    public String login(@PathVariable String usrName, String usrPassword,
                        HttpSession session, Model model){
        SysUser sysUser = sysUserService.login(usrName, usrPassword);
        if(sysUser!=null){//登录成功
            //1.将登录的用户信息保存到session
            session.setAttribute("loginUser",sysUser);
            //2.跳转到首页
            return "redirect:/main";
        }else{//登录失败
            //1.给出错误提示信息
            model.addAttribute("msg","用户名密码错误");
            //2.回到登录页面
            return "forward:/login.jsp";
        }
    }

    /**
     * 跳转首页
     * @return
     */
    @RequestMapping("/main")
    public String main(){
        return "main";
    }
}
